package com.practicario.mvc.model
{
	import com.practicario.mvc.ApplicationFacade;
	import com.practicario.mvc.model.vo.LoginVO;
	import com.practicario.mvc.model.vo.StudentProfileVO;
	import com.practicario.mvc.model.vo.TeacherProfileVO;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
    public class ServicesProxy extends Proxy implements IProxy
    {
		public static const NAME:String = "ServicesProxy";		
		
		private var remoteObject:RemoteObject = new RemoteObject("GenericDestination");
		
		public function ServicesProxy ( data:Object = null ) 
        {
            super ( NAME, data );
        } 
        
        public function updateProfile(perfil:TeacherProfileVO):void
        {
        	remoteObject.source = "UpdateProfile";
        	remoteObject.showBusyCursor = true;
        	remoteObject.ActualizarProfile.addEventListener(FaultEvent.FAULT, onFault);
        	remoteObject.ActualizarProfile.addEventListener(ResultEvent.RESULT, onUpdateProfileResult);
        	remoteObject.ActualizarProfile(perfil);
        }
        
        private function onUpdateProfileResult(e:ResultEvent):void
        {
        	trace(e.result);
        }     
                     
        public function doLogIn(loginVO:LoginVO):void
        {
        	remoteObject.source = "LoginServices";
        	remoteObject.showBusyCursor = true;
        	remoteObject.doLogIn.addEventListener(ResultEvent.RESULT, onLoginResult);
        	remoteObject.doLogIn.addEventListener(FaultEvent.FAULT, onFault);
        	remoteObject.doLogIn(loginVO.usuario, loginVO.password);   	      	
        } 
        
        private function onLoginResult(e:ResultEvent):void
        {
        	//trace(e.result);
        	
        	var tmpArray:Array = e.result as Array;
        	trace(tmpArray[0].perfil.profile);
        	
        	if(e.result!=false)
        	{
        		sendNotification(ApplicationFacade.DO_LOG_IN_SUCCESS, tmpArray);
        	}
        	
        	//guardamos los datos del usuario en el ApplicationFacade para
        	//que puedan ser accedidos desde toda la app
        	
        	/*if(tmpArray[0].perfil.profile=="Profesor")
        	{
        		ApplicationFacade.teacherProfileVO = tmpArray[0].perfil as TeacherProfileVO;
        		trace("soy profesor");
        	}
        	if(tmpArray[0].perfil.profile=="Alumno")
        	{
        		ApplicationFacade.studentProfileVO = tmpArray[0].perfil as StudentProfileVO;
        		trace("soy alumno");
        	}
        	
        	
        	if(e.result!=false)
        	{
        		sendNotification(ApplicationFacade.DO_LOG_IN_SUCCESS, tmpArray);
        	}*/
        } 
        
        private function onFault(e:FaultEvent):void
        {
        	Alert.show(e.fault.faultDetail);
        }            
	}
}